package red.stu.pconstraint.pojo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedList;

/**
 * 流程初始化数据
 * @author yihrmc @stu.red
 * @date 2019/11/21 11:41
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class ProcessStorage {

    /**
     * 未初始化
     */
    public static final Short STATE_UNINITIALIZED = 0;

    /**
     * 流程处理中
     */
    public static final Short STATE_PROCESSING = 3;

    /**
     * 流程挂起
     */
    public static final Short STATE_SUSPEND = 4;

    /**
     * 流程处理完成且通过
     */
    public static final Short STATE_SUCCESS = 10;

    /**
     * 流程处理完成且失败
     */
    public static final Short STATE_FAIL = 11;

    /**
     * 流程实例ID
     */
    @Nullable
    private String processInstanceId;

    /**
     * 流程模板ID
     */
    @NotNull
    private String processId;

    /**
     * 流程状态
     */
    @NotNull
    private Short state = STATE_UNINITIALIZED;

    /**
     * 执行栈 (不可当做历史记录；当流程完成后该数据会被清空)
     */
    @NotNull
    private LinkedList<ExecutionLocation> callStack;

    /**
     * 获取当前执行位置
     * @return
     */
    public ExecutionLocation getCurrentLocation() {
        if (callStack == null || callStack.size() == 0) {
            return null;
        }
        return callStack.getLast();
    }

    public boolean updateCurrentLocation(ExecutionLocation locationDO) {
        if (callStack == null || callStack.size() == 0) {
            return false;
        }
        callStack.removeLast();
        callStack.addLast(locationDO);
        return true;
    }

}
